using UnityEngine;
using QFramework;
using ProjectSurvivor;
using System.Linq;
using QAssetBundle;

namespace QFrameworkSurvivor
{
	public partial class SimpleKnife : ViewController
	{
        void Start()
		{
			// Code Here
		}
        private float mCurrentSeconds = 0;
        private void Update()
        {
            mCurrentSeconds += Time.deltaTime;
            if (mCurrentSeconds > Global.SimpleKnifeDuration.Value)
            {
                mCurrentSeconds = 0;

                var enemies = FindObjectsByType<Enemy>(FindObjectsInactive.Exclude, FindObjectsSortMode.None)
                    .OrderBy(enemy => Player.Default.Distance2D(enemy))
                    .Take(Global.SimpleKnifeCount.Value+Global.AdditionalFlyThingCount.Value);

                var i = 0;
                foreach (var enemy in enemies)
                {
                    if(enemy)
                    {
                        if(i<4)
                        {
                            ActionKit.DelayFrame(i * 11, () => 
                            {
                                AudioKit.PlaySound(Sfx.KNIFE);
                            })
                                .StartGlobal();
                            i++;
                        }
                        Knife.Instantiate()
                        .Position(this.Position())
                        .Show()
                        .Self(self =>
                        {
                            var selfCache = self;
                            if (Global.SuperKnife.Value)
                            {
                                self.LocalScale(2);
                            }
                            self.transform.up = enemy.NormalizedDirection2DFrom(Player.Default);
                            var rigidbody2D = self.GetComponent<Rigidbody2D>();
                            var direction = enemy.NormalizedDirection2DFrom(Player.Default).normalized;
                            var speedTimes = Global.SuperKnife.Value ? Random.Range(2, 3 + 1) : 1;
                            rigidbody2D.velocity = direction * 10*speedTimes;
                            var attackCount = 0;
                            self.OnTriggerEnter2DEvent(collider =>
                            {
                                var hurtBox = collider.GetComponent<HitHurtBox>();
                                if (hurtBox)
                                {
                                    if (hurtBox.Owner.CompareTag("Enemy") && enemy)
                                    {
                                        var damageTimes = Global.SuperKnife.Value ? Random.Range(2, 3 + 1) : 1;
                                        DamageSystem.CalculateDamage(Global.SimpleKnifeDamage.Value* damageTimes, hurtBox.Owner.GetComponent<IEnemy>());
                                        attackCount++;
                                        if(attackCount>=Global.SimpleKnifeAttackCount.Value)
                                        {
                                            self.DestroyGameObjGracefully();
                                        }

                                    }
                                }
                            }).UnRegisterWhenGameObjectDestroyed(selfCache);
                            ActionKit.OnUpdate.Register(() =>
                            {
                                if (Player.Default)
                                {
                                    if (selfCache.Distance2D(Player.Default) >= 15)
                                    {
                                        selfCache.DestroyGameObjGracefully();
                                    }
                                }
                            }).UnRegisterWhenGameObjectDestroyed(selfCache);
                        });
                    }
                }
            }
        }
    }
}
